#!/usr/bin/python

from multiplylib import *

# ============================== for 8x8/16x16 mix multiply ===================l===========
dots_grid_init(cols=16, rows=6)

dots_8x8 = gen_pp(mcand_width=8, mplier_width=8, prefix="", pp0_height_reduction=False)

fig1, ax1 = new_fig("dots_8x8")
display_dots(dots_8x8, style="text")


print_row_formula(dots_8x8, prefix="assign pp_ext")

show()
exit()

mark_dots(start_dot=(0,15), end_dot=(4,0))
mark_dots(start_dot=(0,31), end_dot=(1,16))
mark_dots(start_dot=(2,31), end_dot=(7,16))

dots_8x8_up = reorg_upward(dots_8x8)
fig2, ax2 = new_fig()
display_dots(dots_8x8_up)

# the "1" that could be counteracted
mark_dots(start_dot=(0,31), end_dot=(0,25))
mark_dots(start_dot=(1,25))
for col in range(31,25-1,-1):
    set_dots_cell(dots_8x8_up, 0, col, (0, False))
set_dots_cell(dots_8x8_up, 1, 25, (0, False))
dots_8x8_up = reorg_upward(dots_8x8_up) # re-organized

fig3, ax3 = new_fig()
display_dots(dots_8x8_up)


dots_16x16 = gen_pp(mcand_width=16, mplier_width=16, prefix="")
dots_16x16_up =reorg_upward(dots_16x16)
# display_dots(dots_16x16_up, plot_ax=ax3, fill=False, color="red", style="dot")
display_dots(dots_16x16_up, fill=False, color="red", style="dot")

dots_mix = mix_dots(dots_8x8_up, dots_16x16_up, "mode8")
fig4, ax4 = new_fig()
display_dots(dots_mix, style="text")

print "==================== row formula of 8x8/16x16 mix multiply ===================="
print_row_formula(dots_mix, prefix="assign pp_ext")
print "==============================================================================="

show()
exit()


# ============================== for 8x8/16x16 mix multiply width negate==============================
dots_grid_init(cols=32, rows=10)

dots_8x8_L = gen_pp(mcand_width=8, mplier_width=8, prefix="", prod_negate=True)
# set the last pp (not pp[4] but pp_8_L[4])
pp_8_L_4 = [("pp_8_L[4][%d]" % i,False) for i in range(8)]
set_pp(dots_8x8_L, row=4, pp_val=pp_8_L_4, s_val=("S_8_L[4]", True))
# add sign compensation when PPs of 8x8 mode(16 bits) extend to 32 bits
for col in range(17, 32):
    set_dots_cell(dots_8x8_L, 0, col, ("1'b1", True))
# set_dots_cell(dots_8x8_L, 0, 16, ("{~S4L}", True))
set_dots_cell(dots_8x8_L, 0, 16, ("E_8_L[4]", True))
set_dots_cell(dots_8x8_L, 1, 16, ("Sc", True))

dots_8x8 = copy.deepcopy(dots_8x8_L)
dots_8x8_H = gen_pp(mcand_width=8, mplier_width=8, prefix="", pp_index_start=(4,8), pp0_height_reduction=False, prod_negate=True)
for row in range(7):
    for col in range(16):
        dots_8x8[row+2][col+16] = dots_8x8_H[row][col]


fig1, ax1 = new_fig()
display_dots(dots_8x8, style="text")

mark_dots(start_dot=(0,15), end_dot=(5,0))
mark_dots(start_dot=(0,31), end_dot=(1,16))
mark_dots(start_dot=(2,31), end_dot=(8,16))


dots_8x8_up = reorg_upward(dots_8x8)
fig2, ax2 = new_fig()
display_dots(dots_8x8_up, style="text")
# the "1" that could be counteracted
mark_dots(start_dot=(0,31), end_dot=(0,25))
mark_dots(start_dot=(1,25))
for col in range(31,25-1,-1):
    set_dots_cell(dots_8x8_up, 0, col, (0, False))
set_dots_cell(dots_8x8_up, 1, 25, (0, False))
dots_8x8_up = reorg_upward(dots_8x8_up)

fig3, ax3 = new_fig()
display_dots(dots_8x8_up, style="text")


dots_16x16 = gen_pp(mcand_width=16, mplier_width=16, prefix="", prod_negate=True)
dots_16x16_up =reorg_upward(dots_16x16)
fig4, ax4 = new_fig()
display_dots(dots_16x16_up, style="text")

fig5, ax5 = new_fig()
display_dots(dots_8x8_up, style="dot")
display_dots(dots_16x16_up, fill=False, color="red", style="dot")


dots_mix = mix_dots(dots_8x8_up, dots_16x16_up, "mode8")
fig6, ax6 = new_fig()
display_dots(dots_mix, style="text")


print "==================== row formula of 8x8/16x16 mix multiply with negate===================="
print_row_formula(dots_mix, prefix="assign pp_ext")
print "=========================================================================================="

show()
exit()


# ============================== for dot product ==============================
# init the dots grid params
dots_grid_init(cols=32, rows=12)           # cols,rows

dots_8x8_L = gen_pp(mcand_width=8, mplier_width=8, prefix="", pp0_height_reduction=False)
# set the last pp (not pp[4] but pp_8_L[4])
pp_8_L_4 = [("pp_8_L[4][%d]" % i,False) for i in range(8)]
set_pp(dots_8x8_L, row=5, start_col=8, pp_val=pp_8_L_4)
# fig1, ax1 = new_fig()
# display_dots(ax1, dots_8x8_L, pos_base=(0,1), style="text")

dots_8x8_H = gen_pp(mcand_width=8, mplier_width=8, prefix="", pp_index_start=(4,8), pp0_height_reduction=False)
# PPs for dot product
dots_8x8_dotp = copy.deepcopy(dots_8x8_L)
for row in range(6,12):
    for col in range(16):
        dots_8x8_dotp[row][col] = dots_8x8_H[row-6][col];

fig2, ax2 = new_fig()
display_dots(dots_8x8_dotp, style="text")

mark_dots(start_dot=(0,9))
mark_dots(start_dot=(1,10))
mark_dots(start_dot=(2,12))
mark_dots(start_dot=(3,14))
mark_dots(start_dot=(6,9))
mark_dots(start_dot=(7,10))
mark_dots(start_dot=(8,12))
mark_dots(start_dot=(9,14))

set_dots_cell(dots_8x8_dotp, 0, 9, (0, False))
set_dots_cell(dots_8x8_dotp, 1, 10, (0, False))
set_dots_cell(dots_8x8_dotp, 2, 12, (0, False))
set_dots_cell(dots_8x8_dotp, 3, 14, (0, False))
set_dots_cell(dots_8x8_dotp, 6, 9, (0, False))
set_dots_cell(dots_8x8_dotp, 7, 10, (0, False))
set_dots_cell(dots_8x8_dotp, 8, 12, (0, False))
set_dots_cell(dots_8x8_dotp, 9, 14, (0, False))

set_dots_cell(dots_8x8_dotp, 0, 15, ("1'b1", True))
set_dots_cell(dots_8x8_dotp, 0, 13, ("1'b1", True))
set_dots_cell(dots_8x8_dotp, 0, 11, ("1'b1", True))
set_dots_cell(dots_8x8_dotp, 0, 10, ("1'b1", True))


fig3, ax3 = new_fig()
display_dots(dots_8x8_dotp, style="text")


dots_8x8_dotp_up = reorg_upward(dots_8x8_dotp)
fig4, ax4 = new_fig()
display_dots(dots_8x8_dotp_up, style="dot")

# PPs for 16x16 mode
dots_16x16 = gen_pp(mcand_width=16, mplier_width=16, prefix="")
dots_16x16_up =reorg_upward(dots_16x16)
display_dots(dots_16x16_up, fill=False, color="red", style="dot")

dots_mix = mix_dots(dots_8x8_dotp_up, dots_16x16_up, "mode8")
fig5, ax5 = new_fig()
display_dots(dots_mix, style="text")

print "==================== row formula of dot product ===================="
print_row_formula(dots_mix, prefix="assign pp_ext")
print "===================================================================="

show()
exit()
